/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Controlador.ControlProdServ;
import Modelo.Proveedor;
import Modelo.Servicio;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.swingx.JXSearchField;

/**
 *
 * @author Damian
 */
public class PnlSer extends javax.swing.JPanel {

    /**
     * Creates new form PnlProd
     */
    TableRowSorter<TableModel> rs;
    ArrayList<Proveedor> l;

    public PnlSer() {
        initComponents();
        
        tablaSer.setModel(modeloServicios());

        rs = new TableRowSorter<>(tablaSer.getModel());
        tablaSer.setRowSorter(rs);

        txtBuscar.getDocument().addDocumentListener(
                new DocumentListener() {
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        filtrar();
                    }

                    public void insertUpdate(DocumentEvent e) {
                        filtrar();
                    }

                    public void removeUpdate(DocumentEvent e) {
                        filtrar();
                    }
                });
    }
    
    
    public void actualizar() {
        tablaSer.setRowSorter(null);
        rs = null;
        tablaSer.setModel(modeloServicios());
        rs = new TableRowSorter<>(tablaSer.getModel());
        tablaSer.setRowSorter(rs);
        filtrar();
    }
    public DefaultTableModel modeloServicios() {
        String[] columnas = {"Código", "Nombre", "Descripción", "Precio"};
        DefaultTableModel tabla = new DefaultTableModel(columnas, 0);
        ArrayList<Servicio> lista = new ControlProdServ().listarServicios();
        for (int i = 0; i < lista.size(); i++) {
            Servicio ser = lista.get(i);
            tabla.addRow(new Object[]{ser.getCodigo(), ser.getNombre(),
                ser.getDescripcion(), ser.getPrecio()});
        }
        return tabla;
    }
    public TableRowSorter<TableModel> getRs() {
        return rs;
    }

    public void setRs(TableRowSorter<TableModel> rs) {
        this.rs = rs;
    }

    public ArrayList<Proveedor> getL() {
        return l;
    }

    public void setL(ArrayList<Proveedor> l) {
        this.l = l;
    }

    public JLabel getjLabel1() {
        return jLabel1;
    }

    public void setjLabel1(JLabel jLabel1) {
        this.jLabel1 = jLabel1;
    }

    public JScrollPane getjScrollPane1() {
        return jScrollPane1;
    }

    public void setjScrollPane1(JScrollPane jScrollPane1) {
        this.jScrollPane1 = jScrollPane1;
    }

    public JTable getTablaSer() {
        return tablaSer;
    }

    public void setTablaSer(JTable tablaSer) {
        this.tablaSer = tablaSer;
    }

    public JXSearchField getTxtBuscar() {
        return txtBuscar;
    }

    public void setTxtBuscar(JXSearchField txtBuscar) {
        this.txtBuscar = txtBuscar;
    }

    

    

    private void filtrar() {
        RowFilter<TableModel, Object> rf = null;
        try {
            rf = RowFilter.regexFilter(("(?i)") + txtBuscar.getText(), 1, 2, 5);
        } catch (java.util.regex.PatternSyntaxException e) {
        }
        rs.setRowFilter(rf);
    }

    public int getServSeleccionado() throws ArrayIndexOutOfBoundsException{

        int i = tablaSer.getSelectedRow();
        i = rs.convertRowIndexToModel(i);
        String cod = ((DefaultTableModel) tablaSer.getModel()).getValueAt(i, 0).toString();
        i = Integer.parseInt(cod);
        return i;
    }

    

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tablaSer = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtBuscar = new org.jdesktop.swingx.JXSearchField();

        setLayout(new java.awt.BorderLayout());

        tablaSer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tablaSer);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Buscar:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(67, 67, 67)
                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaSer;
    private org.jdesktop.swingx.JXSearchField txtBuscar;
    // End of variables declaration//GEN-END:variables
}
